package com.winter.collection.List;

import java.util.Iterator;
import java.util.Stack;

/**
 * @program: Collection
 * @description: Stack集合的APi
 * @author: Mr.Liang
 * @create: 2018-01-12 14:54
 **/
public class StackAPI {
    public static void main(String[] args) {
        Stack<Object> stack = new Stack<>();
//        将元素添加到栈中
        for (int i = 0; i < 6; i++) {
            stack.push(String.valueOf(i));
        }
//        遍历栈
        iteratorRandomAccess(stack);
//        查找某个元素中的位置(后进先出的顺序)
        int search = stack.search("2");
        System.out.println(search);

//        pop栈顶元素之后，遍历栈
        stack.pop();
        iteratorRandomAccess(stack);

//        peek栈顶元素，遍历栈
        String peek = (String) stack.peek();
        System.out.println(peek);
        iteratorRandomAccess(stack);

//        通过Iterator遍历Stack
        stackIterator(stack);
    }

    private static void stackIterator(Stack<Object> stack) {
        Iterator<Object> iterator = stack.iterator();
        while (iterator.hasNext()) {
            System.out.println("val:" + iterator.next());
        }
        System.out.println();
    }

    private static void iteratorRandomAccess(Stack<Object> stack) {
        String o;
        for (int i = 0; i < stack.size(); i++) {
            o = (String) stack.get(i);
            System.out.println("val:" + o);
        }
        System.out.println();
    }
}
